#!/usr/bin/env python

# The function of this script is to combine nodes in the same location and generate new node list and element list into a new tcl file 

# ====================== Input tcl files =====================
inputfiles=["/home/chenting/Chenting/bin/FrameTrial/Mesh21.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh22.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh23.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh21l.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh21r.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh22l.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh22r.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh21u.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh21d.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh23u.tcl","/home/chenting/Chenting/bin/FrameTrial/Mesh23d.tcl"]

# ====================== Output tcl files ====================
outputfiles="/home/chenting/Chenting/bin/FrameTrial/NodeEles2.tcl"

# ====================== Input complete =====================

def inornot(v, l):
    for obj in l:
        if type(v[0]) != str and type(obj[0]) != str and type(v[1]) != str and type(obj[1]) != str and type(v[2]) != str and type(obj[2]) != str:
            if (-0.00001 < v[0] - obj[0] < 0.00001):
                if (-0.00001 < v[1] - obj[1] < 0.00001):
                    if -0.00001 < v[2] - obj[2] < 0.00001:
                        return True
    return False

def findindex(v, l):
    count = 0
    for obj in l:
        if type(v[0]) != str and type(obj[0]) != str and type(v[1]) != str and type(obj[1]) != str and type(v[2]) != str and type(obj[2]) != str:
            if (-0.00001 < v[0] - obj[0] < 0.00001):
                if (-0.00001 < v[1] - obj[1] < 0.00001):
                    if -0.00001 < v[2] - obj[2] < 0.00001:
                        return count
        count = count+1
    return False

AllNodeList=[]
AllElementList=[]
for obj in inputfiles:
    content=open(obj)
    NodeList=[]
    ElementList=[]
    for line in content:
        splited=line.split()
        if splited[0]=='node':
            NodeList.append([int(splited[1]),float(splited[2]),float(splited[3]),float(splited[4])])
        elif splited[0]=='element':
            ElementList.append([splited[1],int(splited[2]),int(splited[3]),int(splited[4]),int(splited[5]),int(splited[6]),int(splited[7]),int(splited[8]),int(splited[9]),int(splited[10]),int(splited[11]),int(splited[12])])
    AllNodeList.append(NodeList)
    AllElementList.append(ElementList)
    content.close()

lenAllNodeList=len(AllNodeList)
lenAllElementList=len(AllElementList)

AllNodeCoord=[]
for i in range(0,lenAllNodeList):
    nodecoord=[]
    for obj1 in AllNodeList[i]:
        nodecoord.append([float(obj1[1]),float(obj1[2]),float(obj1[3])])
    AllNodeCoord.append(nodecoord)

for i in range(1, lenAllNodeList):
    numn=len(AllNodeCoord[i])
    nume=len(AllElementList[i])
    for k in range(0, numn):
        for j in range(0, i):
            if inornot(AllNodeCoord[i][k], AllNodeCoord[j]):
                newNodeNumindex=findindex(AllNodeCoord[i][k], AllNodeCoord[j])
                if AllNodeList[j][newNodeNumindex][0]!='canceled':
                    newNodeNum=int(AllNodeList[j][newNodeNumindex][0])
                    print('new',newNodeNum)
                    oldNodeNum=AllNodeList[i][k][0]
                    print('old',oldNodeNum)
                    AllNodeList[i][k][0]='canceled'
                    AllNodeCoord[i][k][0]='canceled'
                    for ii in range(0,nume):
                        for jj in range(2,11):
                            if AllElementList[i][ii][jj]==oldNodeNum:
                                AllElementList[i][ii][jj]=newNodeNum

newf=open(outputfiles,'w')
for i in range(0,lenAllNodeList):
    numn=len(AllNodeCoord[i])
    nume=len(AllElementList[i])
    for k in range(0,numn):
        if AllNodeList[i][k][0]!='canceled':
            newf.write('node %d %f %f %f'%(AllNodeList[i][k][0],AllNodeList[i][k][1],AllNodeList[i][k][2],AllNodeList[i][k][3]))
            newf.write('\n')
            
for i in range(0,lenAllNodeList):
    numn=len(AllNodeCoord[i])
    nume=len(AllElementList[i])        
    for k in range(0,nume):
        newf.write('element %s %d %d %d %d %d %d %d %d %d %d %d'%(AllElementList[i][k][0],AllElementList[i][k][1],AllElementList[i][k][2],AllElementList[i][k][3],AllElementList[i][k][4],AllElementList[i][k][5],AllElementList[i][k][6],AllElementList[i][k][7],AllElementList[i][k][8],AllElementList[i][k][9],AllElementList[i][k][10],AllElementList[i][k][11]))
        newf.write('\n')
        
newf.close()


           
